package com.hpradip.android.documentaryHub;

import java.util.List;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.hpradip.android.documentaryHub.Utils.ImageUtilities;

import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class DocumentaryHub extends Application {

	public static final String TAG = "DocumentryHub";
	public static final String FEED_URL = "http://feeds.topdocumentaryfilms.com/TopDocumentaryFilms";
	private ProgressDialog progressDialog;
	protected List<DocumentaryFeed> documentaryFeeds;
	public static GoogleAnalyticsTracker tracker;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.setAnonymizeIp(true);
		tracker.startNewSession(getResources().getString(R.string.analytics_tracking_code),60,this);
		
	}

	public void loadFeeds(Context context, final Runnable runnable) {
		Runnable feedLoader = new Runnable() {

			public void run() {
				documentaryFeeds = DataProvider.getFeed(FEED_URL);
				for (DocumentaryFeed documentryFeed : documentaryFeeds) {
					if (documentryFeed.getImageThumb() == null && documentryFeed.getImageThumbUri()!=null)
						documentryFeed.setImageThumb(ImageUtilities
								.loadImage(documentryFeed.getImageThumbUri()));
					if (documentryFeed.getImageThumb() == null)
						documentryFeed.setImageThumb(getResources()
								.getDrawable(R.drawable.ancientdiscoveries));
				}

				handler.sendEmptyMessage(0);

			}

			private Handler handler = new Handler() {

				@Override
				public void handleMessage(Message msg) {
					progressDialog.dismiss();
					runnable.run();
				}
			};
		};

			progressDialog = ProgressDialog.show(context, "Please wait",
					"Checking for latest feed updates", true);
			new Thread(feedLoader, "FeedLoader").start();
	}

	public List<DocumentaryFeed> getFeeds(){
		return documentaryFeeds;
	}

	
}
